﻿@using System.Configuration
@using System.Collections

@{
    Layout = "~/_Layout.cshtml";
    Page.Title = "Environment";
    var context = new HttpContextWrapper(HttpContext.Current);
}

<div class="container">
    <h3>Index</h3>
    @{ 
        bool isHidden = String.IsNullOrEmpty(Request.Params["hideSecrets"]) || ("true").Equals(Request.Params["hideSecrets"]); // Hide by default
    }
    @if (Kudu.Core.Helpers.KeyVaultReferenceHelper.NumKeyVaultReferences > 0)
    {
        <div class="checkbox span5">
            <label class="checkbox" style="padding:0px">
                @Html.CheckBox("hideKeyVaultSecrets", isHidden, new { id = "hideKeyVaultSecrets", style = "padding:0px", onchange = "keyVaultCheckbox()" }) Hide Values of KeyVault References
            </label>
        </div>
    }
    <ul>
        <li><a href="#sysInfo">System Info</a></li>
        <li><a href="#appSettings">App Settings</a></li>
        <li><a href="#connectionStrings">Connection Strings</a></li>
        <li><a href="#envVariables">Environment variables</a></li>
        <li><a href="#path">PATH</a></li>
        <li><a href="#httpHeaders">HTTP Headers</a></li>
        <li><a href="#serverVar">Server variables</a></li>
    </ul>
</div>

<div class="container">
    <h3 id="sysInfo">System info</h3>
    <ul>
        <li>System up time: @TimeSpan.FromMilliseconds(Environment.TickCount)</li>
        <li>OS version: @Environment.OSVersion</li>
        <li>64 bit system: @Environment.Is64BitOperatingSystem</li>
        <li>64 bit process: @Environment.Is64BitProcess</li>
        <li>Processor count: @Environment.ProcessorCount</li>
        <li>Machine name: @Environment.MachineName</li>
        <li>Instance id: @Kudu.Core.Infrastructure.InstanceIdUtility.GetInstanceId()</li>
        <li>Short instance id: @Kudu.Core.Infrastructure.InstanceIdUtility.GetShortInstanceId()</li>
        <li>CLR version: @Environment.Version</li>
        <li>System directory: @Environment.SystemDirectory</li>
        <li>Current working directory: @Environment.CurrentDirectory</li>
        <li>IIS command line: @Environment.CommandLine</li>
        @if (Kudu.Core.Helpers.OSDetector.IsOnWindows() && !Kudu.Core.Helpers.EnvironmentHelper.IsWindowsContainers())
        {
            var homePath = Environment.GetEnvironmentVariable("HOME");
            var localPath = Environment.ExpandEnvironmentVariables("%SystemDrive%\\local");
            <li>@homePath usage: @Html.Raw(Kudu.Core.Environment.GetFreeSpaceHtml(homePath))</li>
            if (Kudu.Core.Environment.IsAzureEnvironment() && System.IO.Directory.Exists(localPath))
            {
                <li>@localPath usage: @Html.Raw(@Kudu.Core.Environment.GetFreeSpaceHtml(localPath))</li>
            }
        }
    </ul>

    <h3 id="appSettings">AppSettings</h3>
    @{
        var appSettingsInEnvironment = Kudu.Core.Infrastructure.SettingsProcessor.Instance.AppSettings;
        var appSettingVariablesDict = Kudu.Core.Helpers.KeyVaultReferenceHelper.KeyVaultReferencesFilter(appSettingsInEnvironment, isHidden);
    }
    <ul class="fixed-width">
        @foreach (string name in ConfigurationManager.AppSettings)
         {
            <li>
                @name = @ConfigurationManager.AppSettings[name]
            </li>
         }


        @foreach (KeyValuePair<object, object> kv in appSettingVariablesDict)
         {

            if (kv.Value != null)
            {
                <li>@kv.Key = @kv.Value</li>

            }

         }
    </ul>

    <h3 id="connectionStrings">Connection Strings</h3>
    <ul>
        @foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
         {
            <li>
                <span class="fixed-width">@settings.Name</span>
                <ul class="fixed-width">
                    <li>ConnectionString = @settings.ConnectionString</li>
                    <li>ProviderName = @settings.ProviderName</li>
                </ul>
            </li>
         }
    </ul>

    <h3 id="envVariables">Environment variables</h3>
    @{
        IDictionary environmentVariables = Environment.GetEnvironmentVariables();
        var environmentVariablesDict = Kudu.Core.Helpers.KeyVaultReferenceHelper.KeyVaultReferencesFilter(environmentVariables, isHidden);
    }
    <ul class="fixed-width">
        @foreach (KeyValuePair<object, object> entry in environmentVariablesDict.OfType<KeyValuePair<object, object>>().OrderBy(e => e.Key))
         {
            <li>@entry.Key = @entry.Value</li>
         }
    </ul>

    <h3 id="path">PATH</h3>
    <ul class="fixed-width">
        @foreach (string folder in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator).Where(s => !String.IsNullOrWhiteSpace(s)))
         {
            <li>@folder</li>
         }
    </ul>

    <h3 id="request">Request</h3>
    <ul>
        <li>Url: @Request.Url</li>
    </ul>

    <h3 id="httpHeaders">HTTP headers</h3>
    <ul class="fixed-width">
        @foreach (string name in Request.Headers.OfType<string>().OrderBy(s => s))
         {
            <li>@name=@Request.Headers[name]</li>
         }
    </ul>

    <h3 id="serverVar">Server variables</h3>
    <ul class="fixed-width">
        @foreach (string name in Request.ServerVariables.OfType<string>().OrderBy(s => s))
         {
            <li>@name=@Request.ServerVariables[name]</li>
         }
    </ul>
</div>

<script type="text/javascript">
    // Flip whether to hide KeyVault secrets based on current request; Reload page with new parameter
    function keyVaultCheckbox() {
        var isHidden = '@isHidden' == 'True';
        var flipHidden = (!isHidden).toString();
        var paramName = "hideSecrets";

        // Replace hideSecrets param in URL
        var str = location.search;
        if (new RegExp("[&?]" + paramName + "([=&].+)?$").test(str)) {
            str = str.replace(new RegExp("(?:[&?])" + paramName + "[^&]*", "g"), "")
        }
        str += "&";
        str += paramName + "=" + flipHidden;
        str = "?" + str.slice(1);

        location.assign(location.origin + location.pathname + str + location.hash);
    }
</script>
